
public class DuplicateZeros {
    //复写零
    public void code(int[] arr) {
        int dest = -1, cur = 0;

        //寻找最后一个需要被复写的
        for (; cur < arr.length; cur++){
            if (arr[cur] == 0){
                dest += 2;
            }else{
                dest += 1;
            }

            if (dest >= arr.length - 1){
                break;
            }
        }

        //处理越界问题
        if (dest == arr.length){
            arr[dest - 1] = 0;
            dest -= 2;
            cur--;
        }

        //从右往左写
        for (; cur >= 0; cur--){
            if (arr[cur] == 0){
                arr[dest] = 0;
                arr[dest - 1] = 0;
                dest -= 2;
            }else{
                arr[dest] = arr[cur];
                dest -= 1;
            }
        }
    }

    public static void main(String[] args) {

    }
}
